package jsp.servlet;

import java.io.File;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jpa.entities.Employee;
import jpa.entities.Item;
import jpa.entities.ItemType;
import jsp.classes.Session;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 * @author Remco Haszing
 */
public class Upload extends HttpServlet {

    @Override
    /**
     * Creates an item in the database and writes the file to Catalina home.
     * This method creates an item in the database corresponding to the http
     * request and writes the file to Catalina home on disk. This method parses
     * the following request parameters: dir, project, title, shortdescription,
     * longdescription and file.
     *
     * @param request The request that should be parsed.
     * @param response Unused.
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("wefwef\n\n\n\n\n\n\n\n\n\n");
        try {
            Session session = (Session) request.getSession(true).getAttribute("user");
            String title = "";
            String shortDescription = "";
            String longDescription = "";
            long itemId = -1;
            long parentId = -1;
            long projectId = -1;
            FileItem fi = null;
            for (FileItem i : new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request)) {
                String j = i.getFieldName();
                if (j.equals("dir")) {
                    parentId = Long.parseLong(i.getString());
                } else if (j.equals("item")) {
                    itemId = Long.parseLong(i.getString());
                } else if (j.equals("project")) {
                    projectId = Long.parseLong(i.getString());
                } else if (j.equals("title")) {
                    title = i.getString();
                } else if (j.equals("shortdescription")) {
                    shortDescription = i.getString();
                } else if (j.equals("longdescription")) {
                    longDescription = i.getString();
                } else if (j.equals("file")) {
                    fi = i;
                }
            }
            Item item;
            Employee uploader = session.getEmployee();
            if (itemId != -1) {
                item = session.findProjectItemById(projectId, itemId);
                item.addRevision(fi, uploader);
                session.getJPAConnection().edit(item);
            } else {
                item = new Item(ItemType.MUSIC, title, shortDescription, longDescription, fi, uploader);
                session.findProjectItemById(projectId, parentId).add(item);
                session.getJPAConnection().create(item);
            }
            String dir = session.getUploadDir() + projectId + "/" + item.getId() + "/";
            File d = new File(dir);
            if (!d.exists()) {
                d.mkdirs();
            }
            fi.write(new File(dir + item.getLatestRevision()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
